#include "kernel_operator.h"
#include <cstdint>
#include <cmath>

constexpr int32_t TOTAL_LENGTH = 8 * 2048;
constexpr int32_t USE_CORE_NUM = 8;
constexpr int32_t BLOCK_LENGTH = TOTAL_LENGTH / USE_CORE_NUM; 
constexpr int32_t TILE_NUM = 8;
constexpr int32_t BUFFER_NUM = 2;
constexpr int32_t TILE_LENGTH = BLOCK_LENGTH / TILE_NUM / BUFFER_NUM;

// 定义复数类型
struct complex64 {
    float real;
    float imag;
};

// 自定义的浮点数绝对值函数
__aicore__ inline float Abs(float val) {
    return val > 0.0f ? val : -val;
}

class KernelIsComplex {
public:
    __aicore__ inline KernelIsComplex() {}

    __aicore__ inline void Init(GM_ADDR x, GM_ADDR out) {
        int32_t blockIdx = AscendC::GetBlockIdx();
        xGm.SetGlobalBuffer((__gm__ complex64*)x + blockIdx * BLOCK_LENGTH, BLOCK_LENGTH);
        outGm.SetGlobalBuffer((__gm__ int8_t*)out + blockIdx * BLOCK_LENGTH, BLOCK_LENGTH);

        pipe.InitBuffer(inQueueX, BUFFER_NUM, TILE_LENGTH * sizeof(complex64));
        pipe.InitBuffer(outQueueBool, BUFFER_NUM, TILE_LENGTH * sizeof(int8_t));
    }

    __aicore__ inline void Process() {
        int32_t loopCount = TILE_NUM * BUFFER_NUM;
        for (int32_t i = 0; i < loopCount; i++) {
            CopyIn(i);
            Compute(i);
            CopyOut(i);
        }
    }

private:
    __aicore__ inline void CopyIn(int32_t progress) {
        int32_t tileOffset = progress * TILE_LENGTH;
        if (tileOffset >= BLOCK_LENGTH) return;

        AscendC::LocalTensor<complex64> xLocal = inQueueX.AllocTensor<complex64>();
        

        AscendC::DataCopy(xLocal, xGm[progress * TILE_LENGTH], TILE_LENGTH);
        inQueueX.EnQue(xLocal);

        // AscendC::GlobalTensor<complex64> xGm_tile;
        // xGm_tile.SetGlobalBuffer(const_cast<__gm__ complex64*>(xGm.GetPhyAddr()) + tileOffset, TILE_LENGTH);
        // AscendC::DataCopy(xLocal, xGm_tile, TILE_LENGTH);

        // inQueueX.EnQue(xLocal);
    }

    __aicore__ inline void Compute(int32_t progress) {
        int32_t tileOffset = progress * TILE_LENGTH;
        if (tileOffset >= BLOCK_LENGTH) return;

        AscendC::LocalTensor<complex64> xLocal = inQueueX.DeQue<complex64>();
        AscendC::LocalTensor<int8_t> outLocal = outQueueBool.AllocTensor<int8_t>();
        
        AscendC::LocalTensor<float> xLocal_float = xLocal.ReinterpretCast<float>();

        for (int32_t i = 0; i < TILE_LENGTH; i++) {
            float imag = xLocal_float.GetValue(2 * i + 1);
            outLocal.SetValue(i, (Abs(imag) > 1e-6f) ? 1 : 0);
        }

        outQueueBool.EnQue(outLocal);
        inQueueX.FreeTensor(xLocal);
    }

    __aicore__ inline void CopyOut(int32_t progress) {
        int32_t tileOffset = progress * TILE_LENGTH;
        if (tileOffset >= BLOCK_LENGTH) return;

        AscendC::LocalTensor<int8_t> outLocal = outQueueBool.DeQue<int8_t>();


        AscendC::DataCopy(outGm[progress * TILE_LENGTH], outLocal, TILE_LENGTH);
        outQueueBool.FreeTensor(outLocal);

        // AscendC::GlobalTensor<int8_t> outGm_tile;
        // outGm_tile.SetGlobalBuffer(const_cast<__gm__ int8_t*>(outGm.GetPhyAddr()) + tileOffset, TILE_LENGTH);
        // AscendC::DataCopy(outGm_tile, outLocal, TILE_LENGTH);

        // outQueueBool.FreeTensor(outLocal);
    }
    
private:
    AscendC::TPipe pipe;
    AscendC::TQue<AscendC::TPosition::VECIN, BUFFER_NUM> inQueueX;
    AscendC::TQue<AscendC::TPosition::VECOUT, BUFFER_NUM> outQueueBool;
    AscendC::GlobalTensor<complex64> xGm;
    AscendC::GlobalTensor<int8_t> outGm;
};


extern "C" __global__ __aicore__ void is_complex_custom(GM_ADDR x, GM_ADDR out)
{
    KernelIsComplex op;
    op.Init(x, out);
    op.Process();
}

#ifndef ASCENDC_CPU_DEBUG
void is_complex_custom_do(uint32_t blockDim, void *stream, void *x, void *out)
{
    is_complex_custom<<<blockDim, nullptr, stream>>>(x, out);
}
#endif